<?php
//=========================Class cơ sở dữ liệu=================================
	class csdl
	{
		function csdl($ul="localhost",$name="root",$pass="root",$database="websitebatdongsan")
		{
			mysql_connect($ul,$name,$pass) or die("Không kết nối được với cơ sở dữ liệu");
			mysql_select_db($database);
			mysql_query("SET NAMES 'utf8'");			
		}
		
		function queryCsdl($queryString)
		{
			return (mysql_query($queryString));
		}
		function maxCode($column,$table)
		{
			$n=mysql_query("select max(".$column.") as kqua from ".$table);
			$kq=mysql_fetch_array($n);
			return ($kq["kqua"]);
		}
		function getCode($table,$codename,$len)
		{
			$kq=mysql_query("select max(".$codename.") as max_ma from ".$table);
			$n=mysql_fetch_array($kq);
			$max_ma=$n['max_ma'];
			echo "<br>max_ma: ".$max_ma;
			if($max_ma=='')
				$ma='1';
			else
			{
				$ma=1 + $max_ma;
				//echo "<br>max_ma+1: ".$ma;
			}
			if($len!=0)
			{
				$l=$len-count($ma);
				for($i=0;$i<$l;$i++)
				{
					$ma='0'.$ma;
				}
			}
			//echo "<br>ma: ".$ma;
			//$knoi->queryCsdl("insert into ".$table." values(");
			return $ma;
		}
		function formatDate($date)
		{
			$tam=explode("-",$date);
			$date=$tam[2]."-".$tam[1]."-".$tam[0];
			return $date;
		}
		function upLoad($file_name,$path)
		{
			$tam=basename( $_FILES[$file_name]['name']);
			$tam=explode("%",$tam);
			$tam=implode("",$tam);
			$tam=explode(" ",$tam);
			$tam=implode("",$tam);
			$result='';
			$duongdan ="upload_images\/".$tam;
			if(@move_uploaded_file($_FILES[$file_name]['tmp_name'], $duongdan))
			{
				$result=$duongdan;
			}
			return $result;
		}
		function upLoadResize($name,$path,$width_resize)
		{
			$resize='';
			$path_thumbs=$path;
			$file_type = $_FILES[$name]['type'];
			$file_name = $_FILES[$name]['name'];
			$file_size = $_FILES[$name]['size'];
			$file_tmp = $_FILES[$name]['tmp_name'];
			$ext = strrchr($file_name,'.');
		   $ext = strtolower($ext);
		   $getExt = explode ('.', $file_name);
		   $file_ext = $getExt[count($getExt)-1];
		   $rand_name = md5(time());
		   $rand_name= rand(0,999999999);
		   $ThumbWidth = $width_resize;
		   if($file_size)
		   {
			  if($file_type == "image/pjpeg" || $file_type == "image/jpeg")
			  {
				   $new_img = imagecreatefromjpeg($file_tmp);
			   }
			   elseif($file_type == "image/x-png" || $file_type == "image/png")
			   {
				   $new_img = imagecreatefrompng($file_tmp);
			   }
			   elseif($file_type == "image/gif")
			   {
				   $new_img = imagecreatefromgif($file_tmp);
			   }
			   //list the width and height and keep the height ratio.
			   list($width, $height) = getimagesize($file_tmp);
			   //calculate the image ratio
			   $imgratio=$width/$height;
			   if ($imgratio>1)
			   {
				  $newwidth = $ThumbWidth;
				  $newheight = $ThumbWidth/$imgratio;
			   }
			   else
			   {
					 $newheight = $ThumbWidth;
					 $newwidth = $ThumbWidth*$imgratio;
			   }
			   //function for resize image.
			   if (function_exists(imagecreatetruecolor))
			   {
			   $resized_img = imagecreatetruecolor($newwidth,$newheight);
			   }
			   else
			   {
					 die("Error: Please make sure you have GD library ver 2+");
			   }
			   //the resizing is going on here!
			   imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
			   //finally, save the image
			   ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
			   ImageDestroy ($resized_img);
			   ImageDestroy ($new_img);
			}
			if(@move_uploaded_file ($file_tmp, "$path_thumbs/$rand_name.$file_ext")) 
		   {
			  $resize="$path_thumbs/$rand_name.$file_ext";
		   }
		   return $resize;
	}
}
	//=================Class nhân viên========================
	class nhanvien
	{
		/*manv char(6);
		private hten varchar(50);
		private ngsinh date;
		private cmnd char(9);
		private dthoai varchar(12);
		private dchi varchar(200);
		private chmon varchar(100);
		private struong varchar(200);*/
		function getManv()
		{
			return manv;
		}
		function getHoTen()
		{
			return hten;
		}
		function getNgsinh()
		{
			return ngsinh;
		}
		function getCmnd()
		{
			return cmnd;
		}
		function getDthoai()
		{
			return dthoai;
		}
		function getDchi()
		{
			return dchi;
		}
		function getChmon()
		{
			return chmon;
		}
		function getStruong()
		{
			return struong;
		}
		function queryNhanVien($yeucau)
		{
			return (queryCsdl($yeucau));
		}
		function getMaxManv()
		{
			return (maxCode("manv","nhanvien"));
		}
	}
	//=============Class tour du lịch================================
	class tourdulich
	{
		//private matour char(5);
		
		function queryTourDuLich($yeucau)
		{
			return (queryCsdl($yeucau));
		}
		function getMaxMaTour()
		{
			return (maxCode("matour","tourdulich"));
		}
	}
	//==============Class khách hàng=================================
	
	class khachhang
	{
		
	}
	function setDotNumber($money)
	{
		$gia=explode(".",$money); 
		$money=$gia[0];
		$len=strlen($money);
		$nguyen=(int)($len/3);
		$du=($len%3);
		if($du==0)
		{
			$tien=substr($money,0,3);
			for($i=1;$i<$nguyen;$i++)
				$tien=$tien.".".substr($money,$i*3,($i*3)+3);
		}	
		else
		{
			$tien=substr($money,0,$du);
			for($i=0;$i<$nguyen;$i++)
				$tien=$tien.".".substr($money,$du+($i*3),3);
		}
		return $tien;	
	}
	function getAge($birthday)
	{
		$ht=date('Y-m-d');
		$ngayht=explode("-",$ht);
		$ngsinh=explode("-",$birthday);
		$age=$ngayht[0]-$ngsinh[0];
		if(($ngayht[1]<$ngsinh[1]) ||($ngayht[1]=$ngsinh[1] && $ngayht[2]<$ngsinh[2]))
			$age=$age-1;
		return $age;
	}
	function tongphi($matour,$ngaydi,$tinhphi,$knoi)
	{
		$query=$knoi->queryCsdl("select tongphitour from ngaykhoihanh where matour='$matour' and ngaydi='$ngaydi'");
		$mangtongphitour=mysql_fetch_array($query);
		$tongphitour=$mangtongphitour['tongphitour'];
		$query=$knoi->queryCsdl("select kmai from cokhuyenmai,khuyenmai where cokhuyenmai.stt_km=khuyenmai.stt_km and ngaybd<='$ngaydi' and ngaykt>='$ngaydi' and hinhthuc='1'");
		
		if(mysql_num_rows($query)!=0)
		{	$row=mysql_fetch_array($query);
			$tongphitour=$tongphitour*$row['kmai'];
		}
		$giatien=$tongphitour*$tinhphi;
		return $giatien;
	}
		function kiemtrasession($url)
{	
		//include_once("csdl.php");
		if(isset($_SESSION["manv"]) && isset($_SESSION["quyen"]))
		{
			if($_SESSION["quyen"]==2)
			{
	?>
				<div id="baoquanh">
					<div id="banner">
					<?php include("banner.php"); ?>
				</div>
				<div id="menu">
						<?php include("menunhanvienquantri.php"); ?>
				</div>
				<div id="chinh">
					<div id="left">
						<?php include("left.php"); ?>
					</div>
					<div id="centerhchinh" align="center" ><?php 
					//===================phan than========================
					include($url);
					//====================================================
					 ?></div>
				  </div>
				</div>
	<?php
			}
			else if($_SESSION["quyen"]==1)
			{
	?>
				<meta http-equiv="refresh" content="0;url=indexnhanvien.php" />
	<?php
			}
			else
			{
	?>
				<meta http-equiv="refresh" content="0;url=indexnhanvienhuongdan.php" />
	<?php	
			}
		}
		else
		{
	?>
			<meta http-equiv="refresh" content="0;url=index.php" />
	<?php
		}
}
function testSession()
{
	$menu="";
	if(isset($_SESSION["manv"]) && isset($_SESSION["quyen"]))
	{
			if($_SESSION["quyen"]==2)
				$menu="menunhanvienquantri.php";
			else if($_SESSION["quyen"]==1)
				$menu="menunhanvien.php";
			else
				$menu="menunhanvienhuongdan.php";
	}
	else
		$menu="menunguoidung.php";
	return $menu;
}
/*class customer{
	var $id='';
	var $fullname='';
	var $identitycard='';
	var $address='';
	var $phone='';
	var $mail='';
	function setValue($id,$fullname,$identitycard,$address,$phone,$mail)
	{
			
	}
}*/
?>
